﻿package com.winglish.as3.ui{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	public class WButton{
		static public function GET( $mc: MovieClip, $clickFunction: Function ): WButton{
			if( $mc == null || $clickFunction == null ) return null;
			var btn: WButton = new WButton( Checker.GET );
			btn.init( $mc, $clickFunction );
			return btn; 
		}
		
		private var _button: MovieClip;
		private var _clickFn: Function;
		private var _isActive: Boolean;

		public function WButton( $checker: Checker ) {}
		private function init( $mc: MovieClip, $clickFunction: Function ): void{
			_button = $mc;
			_clickFn = $clickFunction;
			_isActive = false;
			addEvent();
		}
		private function hnClick( $e: MouseEvent ): void{
			_clickFn( this );
			_isActive = true;
			_button.gotoAndStop( 2 );
		}
		private function hnOver( $e: MouseEvent ): void{
			_button.gotoAndStop( 2 );
		}
		private function hnOut( $e: MouseEvent = null ): void{
			if( !_isActive ) _button.gotoAndStop( 1 );
		}
		
		public function addEvent(): void{
			ButtonEvent.addButtonEvent( _button, hnClick, hnOver, hnOut );
			_button.buttonMode = true;
		}
		public function removeEvent(): void{
			ButtonEvent.removeButtonEvent( _button, hnClick, hnOver, hnOut );
			_button.buttonMode = false;
		}
		public function setButtonMode( $boolean: Boolean ): void{
			_button.buttonMode = $boolean;
		}
		public function setActive( $isActive: Boolean = true ): void{
			_isActive = $isActive;
			hnOut();
		}
		public function getActive(): Boolean{
			return _isActive;
		}
		public function getButtonName(): String{
			return _button.name;
		}
	}
}